//
//  HomePageViewController.m
//  DrugLittleHousekeeper
//
//  Created by qianfeng on 15/12/8.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "HomePageViewController.h"

@interface HomePageViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    //表格
    UITableView* _tableView;
    //头部视图
    UIView *_headView;
    //轮播广告的滚动视图
    UIScrollView* _scrollAD;
    //广告的数据源
    NSArray* ADArray;
    //滚动图上的小点点
    UIPageControl *_pageControl;
    //记录表格的滚动
    CGFloat contentOffsetY;
    
    CGFloat newContentOffsetY;
    //记录导航条的透明度
    CGFloat navAlpha;
    

}
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //隐藏导航栏 用自定制的
    self.navigationController.navigationBarHidden=YES;
    
    [self createTableView];
    
    [self createnavTopView];
    
    [self createCustomNav];
}
#pragma mark -  自适应导航条
-(void)createCustomNav
{
    //先把自定制的导航条置为全透明
    [self createNavViewWithAlpha:0];
    //中间加上标题
    UILabel* titleLabel=[MYCustomView createLabelWithFrame:CGRectMake(screen_WIDTH/2-100*scale_Screen/2, 22, 100*scale_Screen, 40*scale_Screen) textString:@"首页" withFont:17.0f textColor:[UIColor blackColor]];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    [self.navView addSubview:titleLabel];
    //扫描二维码加在右边
    UIImageView* QRImageView=[[UIImageView alloc]initWithFrame:CGRectMake(screen_WIDTH-28*scale_Screen-10, 25, 28*scale_Screen, 28*scale_Screen)];
    QRImageView.image=[UIImage imageNamed:@"common_scan"];
    QRImageView.userInteractionEnabled=YES;
    [self.navView addSubview:QRImageView];

}
#pragma mark - 滑动表格改变导航条的透明度
-(void)changeNavAlpha{
    self.navView.backgroundColor=[UIColor colorWithRed:11 / 255.0f green:95 / 255.0f blue:183 / 255.0f alpha:navAlpha/255.0f];
}
#pragma mark - 创建表格视图
-(void)createTableView{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0, screen_WIDTH, screen_HEIGHT-tabBar_Height) style:UITableViewStylePlain];
    _tableView.backgroundColor=[UIColor clearColor];
    //取出分割线
    _tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    _tableView.delegate=self;
    //    _tableView.dataSource=self;
    //取消自适应偏度
    self.automaticallyAdjustsScrollViewInsets=NO;
    //关闭弹性
    _tableView.bounces=NO;
    //去掉分割线
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    //头部视图实例化
    _headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_WIDTH, 200.0f)];
    _headView.backgroundColor=[UIColor clearColor];
    _tableView.tableHeaderView=_headView;
    [self.view addSubview:_tableView];
}
#pragma mark - 表格头视图
-(void)createnavTopView{
    //实例化广告数组  4 1 2 3 4 1
   ADArray=@[@"febe00dc50a4b37c3cc01f64ee8999ec.jpg",@"4a5badde323fd843a5e768b19103ba6c.jpg",@"4d1dddfdc7d7484f1593c5784febca8c.jpg",@"9c9c79fa5a212a8dfe330276838dc51c.jpg",@"febe00dc50a4b37c3cc01f64ee8999ec.jpg",@"4a5badde323fd843a5e768b19103ba6c.jpg"];
    
    //滚动视图实例化
    _scrollAD=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _headView.frame.size.width, _headView.frame.size.height)];
    _scrollAD.showsHorizontalScrollIndicator=NO;
    _scrollAD.showsVerticalScrollIndicator=NO;
    _scrollAD.pagingEnabled=YES;
    _scrollAD.delegate=self;
    _scrollAD.contentSize=CGSizeMake(_scrollAD.frame.size.width*ADArray.count, _scrollAD.frame.size.height);
    
    _scrollAD.contentOffset=CGPointMake(_scrollAD.frame.size.width, 0);
    [_headView addSubview:_scrollAD];
    
        for (int i=0; i<ADArray.count; i++) {
        UIImageView* adImageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*_scrollAD.frame.size.width, 0, _scrollAD.frame.size.width, _scrollAD.frame.size.height)];
            adImageView.image=[UIImage imageNamed:ADArray[i]];
        [_scrollAD addSubview:adImageView];
    }
    //pageCotroll
    _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, _headView.frame.size.height-25, _headView.frame.size.width, 20*scale_Screen)];
    _pageControl.numberOfPages=ADArray.count-2;
    [_headView addSubview:_pageControl];
    //定时器自动轮播
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
#pragma mark - 实现广告轮播
-(void)nextPage{
    [UIView animateWithDuration:0.5f animations:^{
        _scrollAD.contentOffset=CGPointMake(_scrollAD.frame.size.width+_scrollAD.contentOffset.x, 0);
    } completion:^(BOOL finished) {
        if (_scrollAD.contentOffset.x==(ADArray.count-1)*_scrollAD.frame.size.width) {
            _scrollAD.contentOffset=CGPointMake(_scrollAD.frame.size.width, 0);
        }
    }];
}
#pragma mark - 即将开始拖动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_tableView]) {
    contentOffsetY =scrollView.contentOffset.y;
  }
    
}
#pragma mark - 滚动中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //判断表格的滚动方向
    if ([scrollView isEqual:_tableView]) {
        newContentOffsetY=scrollView.contentOffset.y;
        //向上滚动
        if (newContentOffsetY>contentOffsetY) {
            navAlpha=newContentOffsetY;
            if (navAlpha>=255.000000) {
                navAlpha=255.0f;
            }
            //向上滑动到头了
            if (newContentOffsetY+_tableView.frame.size.height==scrollView.contentSize.height) {
                if (newContentOffsetY<255.000000) {
                    navAlpha=255.0f;
                }
            }
            [self changeNavAlpha];
        }else if (newContentOffsetY < contentOffsetY){
            //向下滚动
            navAlpha=newContentOffsetY;
            [self changeNavAlpha];
        }
        
    }else{
    // 4 1 2 3 4 1
    // 0 1 2 3 4
    //   0 1 2 3
        _pageControl.currentPage = _scrollAD.contentOffset.x / _scrollAD.frame.size.width - 1;
    }
}
#pragma mark - 停止滚动   用于手动滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_tableView]) {
        
        
        
    }else{
    if (scrollView.contentOffset.x == (ADArray.count - 1) * scrollView.frame.size.width) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
    }
    
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake((ADArray.count - 2) * scrollView.frame.size.width, 0);
        
    }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
